package cn.iocoder.yudao.module.project.service.electronicscalemanage;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.electronicscalemanage.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.electronicscalemanage.ElectronicScaleManageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.electronicscalemanage.ElectronicScaleManageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 电子秤管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ElectronicScaleManageServiceImpl implements ElectronicScaleManageService {

    @Resource
    private ElectronicScaleManageMapper electronicScaleManageMapper;

    @Override
    public Long createElectronicScaleManage(ElectronicScaleManageSaveReqVO createReqVO) {
        // 插入
        ElectronicScaleManageDO electronicScaleManage = BeanUtils.toBean(createReqVO, ElectronicScaleManageDO.class);
        electronicScaleManageMapper.insert(electronicScaleManage);
        // 返回
        return electronicScaleManage.getId();
    }

    @Override
    public void updateElectronicScaleManage(ElectronicScaleManageSaveReqVO updateReqVO) {
        // 校验存在
        validateElectronicScaleManageExists(updateReqVO.getId());
        // 更新
        ElectronicScaleManageDO updateObj = BeanUtils.toBean(updateReqVO, ElectronicScaleManageDO.class);
        electronicScaleManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteElectronicScaleManage(Long id) {
        // 校验存在
        validateElectronicScaleManageExists(id);
        // 删除
        electronicScaleManageMapper.deleteById(id);
    }

    private void validateElectronicScaleManageExists(Long id) {
        if (electronicScaleManageMapper.selectById(id) == null) {
            throw exception(ELECTRONIC_SCALE_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public ElectronicScaleManageDO getElectronicScaleManage(Long id) {
        return electronicScaleManageMapper.selectById(id);
    }

    @Override
    public PageResult<ElectronicScaleManageDO> getElectronicScaleManagePage(ElectronicScaleManagePageReqVO pageReqVO) {
        return electronicScaleManageMapper.selectPage(pageReqVO);
    }




    @Override
    public void updateStatus(ElectronicScaleManageSaveReqVO updateReqVO) {
        // 校验存在
        validateElectronicScaleManageExists(updateReqVO.getId());
        // 更新状态
        ElectronicScaleManageDO updateObj = new ElectronicScaleManageDO();
        updateObj.setId(updateReqVO.getId());
        updateObj.setStatus(updateReqVO.getStatus());
        electronicScaleManageMapper.updateById(updateObj);
    }

    @Resource
    private RedisTemplate stringRedisTemplate;

    private static final String KEY = "usedScale:";
    @Override
    public ElectronicScaleManageDO getUsedScale(Long userId) {
        String json = (String) stringRedisTemplate.opsForHash().get(KEY, String.valueOf(userId));
        ElectronicScaleManageDO electronicScaleManageDO = JSONObject.parseObject(json, ElectronicScaleManageDO.class);
        return electronicScaleManageDO;
    }

    @Override
    public Boolean updateUsedScale(Long userId, ElectronicScaleManageDO scale) {
        boolean result = false;
        try {
            String jsonString = JSONObject.toJSONString(scale);
            stringRedisTemplate.opsForHash().put(KEY, String.valueOf(userId), jsonString);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}